package com.yichat.micro.msghttpservice.core.metrics;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;

/**
 * Created by chenmohan on 2019/04/23
 *
 * @description
 */
@RestController
@RequestMapping("metrics")
public class MetricController {

    @Autowired
    RequestExecutorMonitor requestExecutorMonitor;

    @Autowired
    RecordExecutorMonitor recordExecutorMonitor;

    @RequestMapping("/request")
    public LinkedHashMap<String, Object> requestMetric() {
        int unusedBucketNum = requestExecutorMonitor.getQueueRemainNumber();
        int activeThreadNum = requestExecutorMonitor.countActiveThread();

        LinkedHashMap<String, Object> result = new LinkedHashMap<>();
        result.put("remian", unusedBucketNum);
        result.put("activeThreadNum", activeThreadNum);
        return result;
    }

    @RequestMapping("/tps")
    public LinkedHashMap<String, Object> tps() {
        LinkedHashMap<String, Object> result = new LinkedHashMap<>();
        result.put("tps", requestExecutorMonitor.tps());
        return result;
    }

    @RequestMapping("/record")
    public LinkedHashMap<String, Object> retryMetric() {
        int unusedBucketNum = recordExecutorMonitor.getQueueRemainNumber();
        int activeThreadNum = recordExecutorMonitor.countActiveThread();

        LinkedHashMap<String, Object> result = new LinkedHashMap<>();
        result.put("remian", unusedBucketNum);
        result.put("activeThreadNum", activeThreadNum);
        return result;
    }
}
